package com.example.a79273.shandianban.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.example.a79273.shandianban.R;
import com.example.a79273.shandianban.application.MyApplication;
import com.example.a79273.shandianban.model.YouLiao02Bean;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshWebView;

import org.xutils.HttpManager;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by lenovo on 2016/8/30.
 */
public class WebFragment extends Fragment {
    private View view;
    private PullToRefreshWebView refreshWebView;
    private WebView mWebView;
    private String mPath;
    private String mContent;
    private RequestParams mParams;
    private HttpManager httpManager = x.http();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_web, null);
        initView();
        initData();
        setData();
        setListener();
        return view;
    }

    private void setListener() {
        refreshWebView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<WebView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<WebView> refreshView) {
                loadData();
                refreshWebView.setRefreshing();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<WebView> refreshView) {

            }
        });
    }

    private void setData() {
        //设置WebView
        mWebView.setWebViewClient(new WebViewClient());
        mWebView.setWebChromeClient(new WebChromeClient());
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setUseWideViewPort(true);

    }

    private void initData() {
        mPath = getArguments().getString("url");
        mParams = new RequestParams(mPath);
        mWebView = refreshWebView.getRefreshableView();
        refreshWebView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        loadData();
    }

    private void initView() {
        refreshWebView = (PullToRefreshWebView) view.findViewById(R.id.wv_web_webview);
    }

    public void loadData() {
        httpManager.get(mParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                YouLiao02Bean bean = YouLiao02Bean.objectFromData(MyApplication.dealWithString(result));
                if (bean != null) {
                    mWebView.loadData(bean.getData().getDetail().getContent(), "text/html", "UTF-8");
                    if (refreshWebView.isRefreshing()) {
                        refreshWebView.onRefreshComplete();
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(getActivity(), "网络出小差啦...", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }
}
